package com.crayon.account.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.crayon.account.ItemDetailFragment;
import com.crayon.account.dao.DAOAccount;
import com.crayon.account.data.ItemList;
import com.crayon.account.impl.OnDialogFragment;
import com.crayon.account.item.adapter.AdapterString;
import com.crayon.sharemenu.R;
import com.crayon.util.UIBIND;
import com.crayon.util.UTILSC;

import java.util.ArrayList;
import java.util.List;

abstract public class FragmentListRoot extends ItemDetailFragment implements OnItemClickListener
,OnDialogFragment
,OnClickListener{

	public abstract List<ItemList> getOnLitemList(List<ItemList> itemarray); 
	public abstract ItemList getOnNewCreateItem();
	public abstract boolean isShownNewCreaetButton();
	AdapterString mAdapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_list_simple,container, false);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ListView listView = (ListView)getView().findViewById(R.id.listView);
		mAdapter = new AdapterString(getActivity());
		listView.setAdapter(mAdapter);
		mAdapter.addAll(getOnLitemList(new ArrayList<ItemList>()));
		listView.setOnItemClickListener(this);
		UIBIND.bindView(getView(), R.id.buttonAddNew, this);

		if(isShownNewCreaetButton()){
			UIBIND.bindViewVisible(getView(), R.id.buttonAddNew, true);
		}

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		ListView listView = (ListView)getView().findViewById(R.id.listView);
		ItemList itemList = (ItemList)listView.getAdapter().getItem(position);
		if(itemList.getTag()!=null){
			UTILSC.addOnPopEditCate(getActivity(), itemList, this);			
		}else{
			DAOAccount item =new DAOAccount(itemList.getType());
			UTILSC.addOnPopWriteFragment(getActivity(), item, null);
		}


	}
	@Override
	public void onDismissDialog(Fragment fragment) {
		// TODO Auto-generated method stub
		mAdapter.clear();
		mAdapter.addAll(getOnLitemList(new ArrayList<ItemList>()));
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.buttonAddNew:
			if(getOnNewCreateItem()!=null){
				ItemList itemList = getOnNewCreateItem();
				if(itemList.getTag()!=null){
					UTILSC.addOnPopEditCate(getActivity(), itemList, this);			
				}
			}
			break;
		}
	}	
}
